<?php
function html_element($PRINT_MODEL)
{
  $POS=0;
  $LEN=strlen($PRINT_MODEL);

  $I=0;
  $J=0;
  while($POS<$LEN)
  {
    $POS=strpos($PRINT_MODEL,"<",$POS);
    if($POS===FALSE)
       break;

    if(substr($PRINT_MODEL,$POS+1,1)=="/")
    {
       $POS+=2;
       continue;
    }

    $POS1=strpos($PRINT_MODEL," ",$POS);
    $POS2=strpos($PRINT_MODEL,">",$POS);

    if($POS1>$POS2)
       $POS1=$POS2;

    $ELEMENT_NAME=substr($PRINT_MODEL,$POS+1,$POS1-$POS-1);
    //echo $ELEMENT_NAME."<br>";

    switch($ELEMENT_NAME)
    {
      case "INPUT":
            $ELEMENT=substr($PRINT_MODEL,$POS,$POS2-$POS+1);
            $ELEMENT_ARRAY[$I++]=$ELEMENT;
            $POS=$POS2+1;
            break;

      case "SELECT":
            $POS2=strpos($PRINT_MODEL,"</SELECT>",$POS2+1);
            $ELEMENT=substr($PRINT_MODEL,$POS,$POS2-$POS+9);
            $ELEMENT_ARRAY[$I++]=$ELEMENT;
            $POS=$POS2+9;
            break;

      case "TEXTAREA":
            $POS2=strpos($PRINT_MODEL,"</TEXTAREA>",$POS2+1);
            $ELEMENT=substr($PRINT_MODEL,$POS,$POS2-$POS+11);
            $ELEMENT_ARRAY[$I++]=$ELEMENT;
            $POS=$POS2+11;
            break;

      case "IMG":
            $ELEMENT=substr($PRINT_MODEL,$POS,$POS2-$POS+1);

            if(get_attr($ELEMENT,"CLASS"))
              $ELEMENT_ARRAY[$I++]=$ELEMENT;
            $POS=$POS2+1;
            break;

      default:
            $POS=$POS2+1;
            break;
    }
  }

  return $ELEMENT_ARRAY;
}

function get_attr($ELEMENT,$ATTR)
{
  //-- 先取元素名 --
  $POS=strpos($ELEMENT," ");
//  echo $POS."<br>";exit;d
  $E_NAME=substr($ELEMENT,1,$POS-1);

  //-- 分类别取值 --
  $ATTR_DATA=="";

  if($ATTR=="NAME")
     $ATTR_DATA=$E_NAME;
  else if($ATTR=="title" || $ATTR=="CLASS" || $ATTR=="DATAFLD" || $ATTR=="DATASRC" || $ATTR=="LV_TITLE" || $ATTR=="LV_SIZE")
  {
     if($ATTR=="title" || $ATTR=="CLASS" )
        $ATTR=strtolower($ATTR);
     else if($ATTR=="DATAFLD")
        $ATTR="dataFld";
     else if($ATTR=="DATASRC")
        $ATTR="dataSrc";

     if(strstr($ELEMENT,$ATTR))
     {
       $POS=strpos($ELEMENT,"$ATTR=")+strlen($ATTR)+1;
       $POS1=strpos($ELEMENT,">",$POS);
       
       if($ATTR=="dataSrc"|| $ATTR=="LV_TITLE" || $ATTR=="LV_SIZE")
       {
          $POS++;
          $POS2=strpos($ELEMENT,"\"",$POS);
       }
       else
          $POS2=strpos($ELEMENT," ",$POS);

       if($POS2<$POS1 && $POS2!=0)
          $POS1=$POS2;

       $ATTR_DATA=substr($ELEMENT,$POS,$POS1-$POS);
       $ATTR_DATA=str_replace("\"","",$ATTR_DATA);
     }
  }
  else if($ATTR=="VALUE")
  {
     if($E_NAME=="INPUT" || $E_NAME=="IMG")
     {
        if(!strstr($ELEMENT,"type=checkbox")) //textfield
        {
     	   $POS=strpos($ELEMENT,"value=")+6;
           $POS1=strpos($ELEMENT,">",$POS);
           $POS2=strpos($ELEMENT," ",$POS);

           if($POS2<$POS1 && $POS2!=0)
              $POS1=$POS2;

           $ATTR_DATA=substr($ELEMENT,$POS,$POS1-$POS);
        }
        else if(strstr($ELEMENT," CHECKED"))
           $ATTR_DATA="on";

        $ATTR_DATA=str_replace("\"","",$ATTR_DATA);
     }
     else if($E_NAME=="TEXTAREA")
     {
     	$POS=strpos($ELEMENT,">")+1;
        $POS1=strpos($ELEMENT,"<",$POS);
        $ATTR_DATA=substr($ELEMENT,$POS,$POS1-$POS);
     }
     else if($E_NAME=="SELECT")
     {
     	$POS=strpos($ELEMENT,">")+1;
        $POS1=strpos($ELEMENT,"</SELECT>",$POS);
        $ATTR_DATA=substr($ELEMENT,$POS,$POS1-$POS);
     }
 }

 return $ATTR_DATA;
}
?>
